﻿using System;
using System.Collections.Generic;

namespace FastEmit
{
    public class ConstructorAccessor
    {
        private readonly Dictionary<int, ConstructorInvoker> dict = [];

        public ConstructorAccessor(Type type)
        {
            var list = type.GetConstructors();
            foreach (var item in list)
            {
                var invoker = ConstructorInfoExtensions.DelegateForCreateInstance(item);
                dict.Add(item.GetParameters().Length, invoker);
            }
        }

        public ConstructorInvoker GetInvoker(params object[] parameters)
        {
            return dict[parameters.Length];
        }

        public object CreateInstance(params object[] parameters)
        {
            if (parameters.Length == 0)
            {
                return dict[0]();
            }
            return dict[parameters.Length](parameters);
        }

    }
}
